//获取其他服务器的数据
const http = require("http");
const fs = require("fs");
const cheerio = require("cheerio"); //第三方模块 需要安装

let webUrl = "http://www.atguigu.com/";

http.get(webUrl, (req) => {
  //通过流的方式获取到请求的数据
  let str = "";
  //监听流的数据
  req.on("data", (chunk) => {
    // console.log(chunk);
    str += chunk;
  });
  // 流数据传递结束
  req.on("end", () => {
    // console.log(str);
    // fs.writeFileSync("test.html", str);
    let $ = cheerio.load(str);
    let arr = [];
    let id = 1;
    $(".rr a").each((key, item) => {
      let text = $(item).text();
      arr.push({
        id: id++,
        content: text,
      });
    });
    // console.log(arr);
    fs.writeFileSync("./data.json", JSON.stringify(arr));
  });
});
